set(CMAKE_REQUIRED_LIBRARIES_OLD ${CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_INCLUDES_OLD ${CMAKE_REQUIRED_INCLUDES})
check_library_exists(SDL2 SDL_GL_CreateContext "" HAVE_SDL2_LIB)
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_OLD})
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES_OLD})
if(HAVE_SDL2_LIB)
  add_executable(sdlplay ${APPTYPE} sdlplay.cpp)
  target_link_libraries(sdlplay PRIVATE mdk SDL2main SDL2)
  if(NOT WIN32)
    target_link_libraries(sdlplay PRIVATE dl)
  endif()
  if(WIN32)
    target_link_libraries(sdlplay PRIVATE winmm imm32 ole32 version) # for static sdl
  elseif(APPLE)
    target_link_libraries(sdlplay PRIVATE iconv "-framework CoreFoundation" "-framework CoreGraphics" "-framework Foundation" "-framework AppKit" "-framework IOKit" "-framework AudioToolbox" "-framework CoreAudio" "-framework ForceFeedback" "-framework Metal" "-framework CoreVideo" "-framework CoreServices" "-framework Carbon") # for static sdl
  endif()
  if(EXISTS /usr/local/include/SDL2)
    target_include_directories(sdlplay PRIVATE /usr/local/include/SDL2)
  elseif(EXISTS /usr/include/SDL2)
    target_include_directories(sdlplay PRIVATE /usr/include/SDL2)
  endif()
  install(TARGETS sdlplay RUNTIME DESTINATION bin)
endif()